<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height:60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="hAdd"
              >新增角色</el-button>
            </el-row>
            <!-- 表格 -->
            <el-table
              :data="list"
            >
              <el-table-column label="序号" type="index" width="50" />
              <el-table-column label="角色名称" property="name" width="240" />
              <el-table-column label="描述" property="description" />
              <el-table-column label="操作" width="240">
                <template slot-scope="scope">
                  <el-button size="small" type="success">分配权限</el-button>
                  <el-button size="small" type="primary" @click="hChange(scope)">编辑</el-button>
                  <el-button size="small" type="danger" @click="hDelete(scope.row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <el-row type="flex" justify="center" align="middle" style="height: 60px">
              <!-- 分页组件 -->
              <div class="block">
                <el-pagination
                  :current-page="listForm.page"
                  :page-sizes="[2, 3, 5, 10]"
                  :page-size="2"
                  layout="total, sizes, prev, pager, next, jumper"
                  :total="listForm.total"
                  @size-change="handleSizeChange"
                  @current-change="handleCurrentChange"
                />
              </div>
            </el-row>
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
    <el-dialog
      :title="titleName"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :visible.sync="showDialog"
      @close="dialogClose"
    >
      <el-form ref="roleForm" :model="roleForm" :rules="rules" label-width="100px">
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="roleForm.name" />
        </el-form-item>
        <el-form-item label="角色描述" prop="description">
          <el-input v-model="roleForm.description" />
        </el-form-item>
      </el-form>
      <!-- 底部 -->
      <el-row slot="footer" type="flex" justify="center">
        <el-col :span="6">
          <el-button size="small" @click="showDialog=false">取消</el-button>
          <el-button size="small" type="primary" @click="hSubmit">确定</el-button>
        </el-col>
      </el-row>
    </el-dialog>
  </div>
</template>
<script>
import { addRoles, delRoles, getRoles, getRolesDetail, updateRoles } from '@/api/setting'
export default {
  data() {
    return {
      showDialog: false,
      changeH: false,
      list: [],
      listForm: {
        page: 1,
        pagesize: 2,
        total: 0
      },
      roleForm: {
        name: '',
        description: ''
      },
      resForm: {
        name: '',
        description: ''
      },
      rules: {
        name: [{ required: true, message: '年龄不能为空', trigger: 'blur' }],
        description: [{ required: true, message: '年龄不能为空', trigger: 'blur' }]
      }
    }
  },
  computed: {
    titleName() {
      return this.changeH ? '编辑角色' : '新增角色'
    },
    maxNum() {
      return Math.ceil(this.listForm.total / this.listForm.pagesize)
    },
    isLastPageFulled() {
      return this.listForm.total % this.listForm.pagesize === 0
    }
  },
  // watch: {
  //   showDialog: {
  //     handler() {
  //       this.$refs.roleForm.resetFields()
  //     }
  //   }
  // },

  created() {
    this.loadList()
  },
  methods: {
    dialogClose() {
      this.$refs.roleForm.resetFields()
    },
    hDelete(id) {
      delRoles(id)
      if (this.list.length <= 1) this.listForm.page--
      this.loadList()
    },
    async hChange(scope) {
      const res = await getRolesDetail(scope.row.id)
      this.roleForm = res.data
      this.showDialog = true
      this.changeH = true
    },
    hAdd() {
      // if (this.list.length === this.listForm.pagesize) this.listForm.page++
      let Num = this.maxNum
      if (this.isLastPageFulled) {
        Num++
        this.listForm.total++
      }
      // console.log(Num)
      this.listForm.page = Num
      this.roleForm = { ...this.resForm }
      this.changeH = false
      this.showDialog = true
    },
    hSubmit() {
      this.$refs.roleForm.validate((vali) => {
        if (!vali) return
        this.changeH ? updateRoles(this.roleForm) : addRoles(this.roleForm)
        this.showDialog = false
        this.loadList()
      })
    },
    async loadList() {
      const res = await getRoles(this.listForm)
      this.list = res.data.rows
      this.listForm.total = res.data.total
      console.log(res)
    },
    handleSizeChange(val) {
      this.listForm.pagesize = val
      this.loadList()
    },
    handleCurrentChange(val) {
      this.listForm.page = val
      this.loadList()
    }
  }
}
</script>
